package com.enwei.activitys;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.enwei.base.BaseActivity;
import com.enwei.utils.CacheKeys;
import com.enwei.utils.Constans;
import com.enwei.utils.SpfHelper;
import com.enwei.utils.http.HttpApi;
import com.enwei.utils.http.HttpCallBack;
import com.ew.action.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class CasesActivity extends BaseActivity{
	

	@ViewInject(R.id.scrollView)ScrollView mScrollView;
	@ViewInject(R.id.case_layout)LinearLayout mCaseLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cases,true,true);
		ViewUtils.inject(this);
		requestData();
	}

	
	public void requestData(){
		String url = Constans.USER_CK;
		String phone = (String) SpfHelper.getParam(this, CacheKeys.LOGIN_USER_HOME, "");
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("phone", phone));
		HttpApi http = new HttpApi(this,HttpMethod.POST);
		http.sendHttpRequest(url, nameValuePairs, new HttpCallBack() {
			@Override
			public void onSuccess(String data) {
				try {
					getLoadingView().setVisibility(View.GONE);
					if(!TextUtils.isEmpty(data)){
						JSONArray arrays = new JSONArray(data);
						bindData(arrays);
						mScrollView.setVisibility(View.VISIBLE);
					}else{
						getEmpty().setVisibility(View.VISIBLE);
					}
				} catch (JSONException e) {
					getErrorLayout().setVisibility(View.VISIBLE);
					e.printStackTrace();
				}
				
			}
			@Override
			public void onFailure() {
				getLoadingView().setVisibility(View.GONE);
				getErrorLayout().setVisibility(View.VISIBLE);
				super.onFailure();
			}
		});
	}
	
	public void bindData(JSONArray arrays){
		if(arrays != null && arrays.length() > 0){
			mCaseLayout.removeAllViews();
			try {
				for (int i = 0 ;i < arrays.length(); i++) {
					View v = LayoutInflater.from(this).inflate(R.layout.view_cases_info_cell, null);
					TextView cases_content = (TextView) v.findViewById(R.id.cases_content);
					TextView time = (TextView) v.findViewById(R.id.time);
					TextView title =  (TextView) v.findViewById(R.id.title);
					JSONObject jsonObj = arrays.getJSONObject(i);
					cases_content.setText(jsonObj.getString("content"));
					time.setText(jsonObj.getString("time"));
					title.setText(jsonObj.getString("title"));
					mCaseLayout.addView(v);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	protected void onRetry() {
		mScrollView.setVisibility(View.GONE);
		getLoadingView().setVisibility(View.VISIBLE);
		getErrorLayout().setVisibility(View.GONE);
		requestData();
		super.onRetry();
	}
	
}